import json
import re
from typing import List


def is_json(invoke):
    try:
        json.loads(invoke)
    except ValueError:
        return False
    return True

def check_action_invoke_structure(action: str):
    valid = 1
    think_pattern = r"</think>"
    think_match = re.search(think_pattern, action, re.DOTALL)
    if not think_match:
        valid = 0
    after_think = action[think_match.end()]

    if "<action>" in after_think:
        action_count = after_think.count("<action>")
        invoke_count = after_think.count("<invoke>")
        #如果think后出现<action>，如果没出现</action>，<action>词条数不为1，invoke词条数目不为1，action不以</invoke>结尾，那么valid=0
        if "</action>" not in after_think and action_count != 1 and invoke_count != 1 and not action.strip().endswith("</invoke>"):
            valid = 0
    return valid


def swe_bench_projection(action):
    """
    A function to process the actions.
    actions: the list of actions to be processed, it is a list of strings.
    Expected format:
        <think>some reasoning...</think><action>up/down/left/right/still</action>
    """    
    pattern_steps = [r".*```(.*)```.*",r".*```bash(.*)```.*",r".*```shell(.*)```.*"]

    valid = 0
    command = {}
    match_steps = [re.match(p,action,flags=re.DOTALL) for p in pattern_steps]
    for match_step in match_steps:
        if match_step:
            valid = 1
            command["command"] = match_step.group(1)

    return command, valid

def swe_bench_mem_projection(action):
    """
    A function to process the actions.
    actions: the list of actions to be processed, it is a list of strings.
    Expected format:
        <think>some reasoning...</think><action>up/down/left/right/still</action>
    """
    # print(f"### action 0 {actions[0]}")
    
    pattern_step = r"^\s*<think>\s*(.*)\s*</think>\s*<action>\s*(.*)\s*</action>\s*<memory>\s*(.*)\s*</memory>.*$"
    # pattern_answer = r"^\s*<answer>\s*(.*)\s*</answer>\s*$"
    
    
    valid = 1
    
    match_step = re.match(pattern_step,action,flags=re.DOTALL)
    # match_answer = re.match(pattern_answer,action,flags=re.DOTALL)
    if match_step:
        _think = match_step.group(1)
        _action = match_step.group(2)
        _memory = match_step.group(3)
        
        _actions_results, _valids = swe_bench_projection(_action)
        _action = _actions_results
        valid = _valids
    else:
        _think = "error"
        _memory = "error"
        _action = {"command":"error"}
        valid = 0
    _action["memory"] = _memory

    return _action, valid

if __name__ == "__main__":
    test_list = [
        "\n <think> eqw\neh \n</think>\n<action> qw\neqweqwe </action>\n<invoke> {\"aaa\":\n\"bb\"} </invoke>\n",
        "\n <think> eqw\neh \n</think>\n<action> qweqweqwe </action>\n<invoke> qqqqqqq </invoke>\n<", 
        "\n <answer> eqweh \n</think>\n<action> qweqweqwe </action>\n<invoke> qqqqqqq </answer>\n<",
        "\n <answer> eqweh  qqqqqqq </answer>\n",
        "<answer>\n eqweh \n qqqqqq1q \n</answer>\n",
        "\n <answer> eqweh </think>\n<action> qqqqqqq </answer>\n",   
    ]
    res = [1,0,0,1,1,1]
    
    print(swe_bench_projection(test_list))
